<?php
// +----------------------------------------------------------------------
// | Quotes [ 只为给用户更好的体验]**[我知道发出来有人会盗用,但请您留版权]
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 零度  <2109877665@qq.com>          盗用不留版权,你就不配拿去!
// +----------------------------------------------------------------------
// | Date: 2019年08月20日
// +----------------------------------------------------------------------
include("./Core/Common.php");
if(count($_GET))$queryArr=$_GET;else$queryArr=$_POST;

$url=daddslashes($queryArr['url']);
$authcode=daddslashes($queryArr['authcode']);
$Instant_pid=daddslashes($queryArr['Instant_pid']);
$Instant_key=daddslashes($queryArr['Instant_key']);
$Coheck_row=$DB->query("SELECT * FROM pay_check WHERE url='{$url}' limit 1")->fetch();
$Instant_row=$DB->query("SELECT * FROM pay_cloud WHERE qq='{$Coheck_row['qq']}' limit 1")->fetch();
if(!$Instant_pid){
	exit(json_encode(array("code"=>-5,"msg"=>"云端PID未输入")));
}elseif(!$Instant_key){
	exit(json_encode(array("code"=>-6,"msg"=>"云端KEY未输入")));
}elseif(!$Coheck_row){
	exit(json_encode(array("code"=>-7,"msg"=>"授权域名不存在")));
}elseif($Instant_row['key']!=$Instant_key){
	exit(json_encode(array("code"=>-8,"msg"=>"云端KEY错误")));
}elseif($Coheck_row['status']==0){
	exit(json_encode(array("code"=>-8,"msg"=>"授权已被禁封")));
}elseif(!checkauth_pid_key($url,$authcode,$Instant_pid,$Instant_key)){
	exit(json_encode(array("code"=>-9,"msg"=>"信息不匹配")));
}


$act=isset($queryArr['act'])?daddslashes($queryArr['act']):null;
if($act=='Add_Qrcode'){//二维码添加
    $image_field = $_FILES['img']['tmp_name'];
	$pid=daddslashes($queryArr['pid']);
	$Add = move_uploaded_file($image_field, ROOT.'QRCODE/'.$pid.'.png');
	if($_FILES==false or $pid==false){
		$result=array("code"=>-1,"msg"=>"添加二维码失败,图片数据为空","qrcode"=>false);
	}elseif($Add){
		if($qrcode = qrcode('http://'.$_SERVER['HTTP_HOST'].'/QRCODE/'.$pid.'.png')){
			$result=array("code"=>1,"msg"=>"添加并解码成功","qrcode"=>$qrcode);
		}elseif($qrcode = qrcode('http://'.$_SERVER['HTTP_HOST'].'/QRCODE/'.$pid.'.png')){
			$result=array("code"=>1,"msg"=>"添加并解码成功","qrcode"=>$qrcode);
		}elseif($qrcode = qrcode('http://'.$_SERVER['HTTP_HOST'].'/QRCODE/'.$pid.'.png')){
			$result=array("code"=>1,"msg"=>"添加并解码成功","qrcode"=>$qrcode);
		}else{
			$result=array("code"=>-1,"msg"=>"二维码解码失败","qrcode"=>false);
		}
	}else{
		$result=array("code"=>-1,"msg"=>"添加二维码失败,请重试","qrcode"=>false);
	}
	unlink(ROOT.'QRCODE/'.$pid.'.png');  //解码后清理二维码图片 
}elseif($act=='Get_Money_Notify'){//添加待回调数据
	$api_type=daddslashes($queryArr['api_type']);
	$money=daddslashes($queryArr['money']);
	$money = round($money,2);
	$type=daddslashes(explode('_',$api_type)[1]);
	if($money<0.00){
		$result=array("code"=>-4,"msg"=>"请输入正确的对比金额");
	}elseif(!strstr($type, 'alipay') and !strstr($type, 'qqpay') and !strstr($type, 'wxpay')){
		$result=array("code"=>-3,"msg"=>"请输入正确的类型");
	}else{
		if($money>=0.01){//判断是否是收入金额
			$Sql = $DB->query("insert into `pay_notify` (`url`,`api_type`,`money`,`pay_msg`,`status`,`nums`,`crontime`,`addtime`) values ('".$url."','".$api_type."','".$money."','暂未回调','0','0','0','".$date."')");
			$id=$DB->lastInsertId();
			$row=$DB->query("SELECT * from `pay_notify` WHERE id='{$id}'and status='0' order by rand() limit 1")->fetch();
			$data = Get_Notify($row);//向各大前台发送回调
		}
		if($Sql){
		  

			$result=array("code"=>1,"msg"=>"提交回调请求成功,等待系统响应,云端订单编号:".$id);
		}else{
			$result=array("code"=>-1,"msg"=>"提交失败");
		}
	}
}elseif($act=='Query'){	//获取云端信息
	//$result=array("code"=>1,"msg"=>"获取云端信息成功","money"=>$Instant_row['money'],"qq"=>$Coheck_row['qq'],"beizhu"=>$Coheck_row['beizhu'],"status"=>$Coheck_row['status'],"addtime"=>$Coheck_row['addtime']);
	$Instant_row['code'] = 1;
	$Instant_row['msg'] = "获取云端信息成功";
	$Instant_row['ed_money'] = $conf['ed_money'];
	$Instant_row['alipay_up_money'] = $conf['alipay_up_money'];
	$Instant_row['status'] = $Coheck_row['status'];
	$Instant_row['pc_check_money'] = $conf['pc_check_money'];
	$result = $Instant_row;
}elseif($act=='Submit'){	//发起支付
	$pay_id=daddslashes($queryArr['pay_id']);
	$outtime=daddslashes($queryArr['outtime']);
	$trade_no=daddslashes($queryArr['trade_no']);
	$out_trade_no=daddslashes($queryArr['out_trade_no']);
	$notify_url=daddslashes($queryArr['notify_url']);
	$type=daddslashes($queryArr['type']);
	$name=daddslashes($queryArr['name']);
	$money=daddslashes($queryArr['money']);
	$sign=daddslashes($queryArr['sign']);

	if(strpos($type,'alipay') or strpos($type,'qqpay') or $type){
		if($money>$Instant_row['money']){
			$result=array("code"=>-1,"msg"=>"当前站点云端额度不能够发起这个金额的订单,当前云端额度：".$Instant_row['money']." 请提醒站长充值!","price"=>'-0.01');
		}else{
			$DB->query("insert into `pay_intl_order` (`out_trade_no`,`intl_pid`,`notify_url`,`type`,`money`,`addtime`,`status`) values ('".$out_trade_no."','".$Instant_pid."','".$notify_url."','".$type."','".$money."','".$date."','0')");
			$notify_url		= daddslashes(($_SERVER['SERVER_PORT'] == '443' ? 'https://' : 'http://').$_SERVER['HTTP_HOST'].'/notify_url_api.php');
			if($pay_id and $outtime and $trade_no and $out_trade_no and $notify_url and $type and $name and $money and $sign)
			$arr = $Instant_Api->Submit($pay_id,$outtime,$trade_no,$out_trade_no,$notify_url,$type,$name,$money,$sign);
			$result=array("code"=>$arr['code'],"msg"=>$arr['msg'],"price"=>$arr['price']);
		}
	}else{
		$DB->query("insert into `pay_intl_order` (`out_trade_no`,`intl_pid`,`notify_url`,`type`,`money`,`addtime`,`status`) values ('".$out_trade_no."','".$Instant_pid."','".$notify_url."','".$type."','".$money."','".$date."','0')");
		$notify_url		= daddslashes(($_SERVER['SERVER_PORT'] == '443' ? 'https://' : 'http://').$_SERVER['HTTP_HOST'].'/notify_url_api.php');
		if($pay_id and $outtime and $trade_no and $out_trade_no and $notify_url and $type and $name and $money and $sign)$arr = $Instant_Api->Submit($pay_id,$outtime,$trade_no,$out_trade_no,$notify_url,$type,$name,$money,$sign);
		$result=array("code"=>$arr['code'],"msg"=>$arr['msg'],"price"=>$arr['price']);
	}
}elseif($act=='getNotice'){
    if($conf['notice_state']=='on'){
        $result=[
    	    "code"=>1,
    	    "data"=>[
    	        "notice"=>$conf['notice'],
    	        "notice_update_time"=>$conf['notice_update_time'],
    	    ]
    	];
    }else{
         $result=[
    	    "code"=>-1,
    	    "data"=>[
    	        "notice"=>null,
    	    ]
    	];
    }
    	
    
}else{
	$result=array("code"=>-200,"msg"=>"参数错误",'param'=>$queryArr);
}
if($result)
	exit(json_encode($result));
else
	exit($data);
?>